<?php
include("config/connect.php");
include("public/common.php");
$opt = trim(CheckUrlParamNoDie("opt"));
$ref = urldecode(trim(CheckUrlParamNoDie("ref")));
if ($opt == "logout")
{
	setcookie("gwt_userid", "",time()+100*3600*24, "/");
	setcookie("gwt_zfb", "",time()+100*3600*24, "/");
	setcookie("gwt_nickname", "",time()+100*3600*24, "/");
	setcookie("gwt_phone", "",time()+100*3600*24, "/");
	setcookie("gwt_email", "",time()+100*3600*24, "/");
	
	setcookie("gwt_userid", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_zfb", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_nickname", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_phone", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_email", "",time()+100*3600*24, "/",".gouwutu.com");
	header("location:new/");
	exit;
}
if ($opt == "qqpluslogout")
{
	setcookie("gwt_userid", "",time()+100*3600*24, "/");
	setcookie("gwt_zfb", "",time()+100*3600*24, "/");
	setcookie("gwt_nickname", "",time()+100*3600*24, "/");
	setcookie("gwt_phone", "",time()+100*3600*24, "/");
	setcookie("gwt_email", "",time()+100*3600*24, "/");
	
	setcookie("gwt_userid", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_zfb", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_nickname", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_phone", "",time()+100*3600*24, "/",".gouwutu.com");
	setcookie("gwt_email", "",time()+100*3600*24, "/",".gouwutu.com");
	header("location:new/qqplus_index.php");
	exit;
}

//
$zfbzh = trim(GetPostParamStr("zfbzh"));
if ($zfbzh == "")
{
	echo "error";
	exit;
}

$email_tel = trim(GetPostParamStr("email_tel"));
$use_phone = GetPostParamInt("use_phone");
if($use_phone)
{
	$phone = $zfbzh;
	$email = $email_tel;
}
else
{
	$phone = $email_tel;
	$email = $zfbzh;
}

$commend_id = trim(GetPostParamStr("commend_id"));
$commend_id = GetRecommendIdFromCode($commend_id, $type);

$type = "N";
if ($commend_id != "")
{
	$result = mysql_query( "select type from user where id=$commend_id limit 1" );
	$row = mysql_fetch_assoc( $result );
	$type = strtoupper($row["type"]);
	if ($type == 'U' || $type == 'Y')
		$type = 'N';
	else if ($type == 'V')
		$type = 'Y';
	else
		$type = 'N';
}

$result = mysql_query( "select id from user where tb_id='$zfbzh' limit 1" );
$bInsert = mysql_num_rows( $result ) == 0;

//
if (($use_phone && $email=="") || (!$use_phone && $phone==""))
	$sql = "INSERT INTO user (tb_id, phone, email, commend_id, type, new_flag) values('$zfbzh', '$phone', '$email', '$commend_id', '$type', 1)";
else
	$sql = "INSERT INTO user (tb_id, phone, email, commend_id, type, new_flag) values('$zfbzh', '$phone', '$email', '$commend_id', '$type', 1) ON DUPLICATE KEY UPDATE phone='$phone',email='$email'";
//echo $sql;
mysql_query($sql);
$result = mysql_query( "select id from user where tb_id='$zfbzh' limit 1" );
$row = mysql_fetch_assoc($result);
$userid = $row["id"];

// 消息
if ($bInsert)
{
	if ($commend_id=="ICBC95588")
	{
		mysql_query( "update user set level=level+100,tubi=tubi+100 where id=$userid" );
		mysql_query( "insert into gwt_msg (touser,content) values ($userid,'尊敬的 ".$zfbzh."，恭喜您已成功注册购物兔，您是工行用户，额外赠送您200兔币，祝您购物愉快！')" );
	}
	else
		mysql_query( "insert into gwt_msg (touser,content) values ($userid,'尊敬的 ".$zfbzh."，恭喜您已成功注册购物兔，赠送您100兔币，祝您购物愉快！')" );
}

// 临时往活动里插入一条数据
if ($bInsert && $commend_id != "")
{
	$result = mysql_query( "select id from hd_lottery where user_id=$commend_id limit 1" );
	if(mysql_num_rows( $result ) > 0)	// 判断用户是否参加活动
	{
		$rndNum = rand(0000000,9999999);
		$result = mysql_query( "insert into hd_lottery (user_id,invitees_id,lottery_num) values ($commend_id, $userid, '$rndNum')" );
	}
}

//echo $userid;
setcookie("gwt_userid", "$userid",time()+100*3600*24, "/",".gouwutu.com");
setcookie("gwt_zfb", "$zfbzh",time()+100*3600*24, "/",".gouwutu.com");
setcookie("gwt_phone", "$phone",time()+100*3600*24, "/",".gouwutu.com");
setcookie("gwt_email", "$email",time()+100*3600*24, "/",".gouwutu.com");

if ($ref=="")
	header("location:new/");
else
	header("location:http://$ref");

?>